package hidalgo.expenses.activity;

import hidalgo.expenses.R;
import hidalgo.expenses.SessionManager;
import hidalgo.expenses.utils.MailTest;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class MenuActivity extends BaseActivity {

	private Button quickNote;
	private Button addExpense;
	private Button addIncome;
	private Button accounts;
	private Button balance;
	private Button btnLogout;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.menu_activity);

		quickNote = (Button) findViewById(R.id.quickNote);
		addExpense = (Button) findViewById(R.id.addExpense);
		addIncome = (Button) findViewById(R.id.addIncome);
		accounts = (Button) findViewById(R.id.accounts);
		balance = (Button) findViewById(R.id.balance);
		btnLogout = (Button) findViewById(R.id.btnLogout);

		quickNote.setOnClickListener(new OnClickListener() {
			public void onClick(View view) {

				Intent intent = new Intent(MenuActivity.this,
						QuickNoteActivity.class);
				startActivity(intent);

			}
		});

		addExpense.setOnClickListener(new OnClickListener() {
			public void onClick(View view) {
				Intent intent = new Intent(MenuActivity.this,
						AddExpenseActivity.class);
				startActivity(intent);
			}
		});

		addIncome.setOnClickListener(new OnClickListener() {
			public void onClick(View view) {
				Intent intent = new Intent(MenuActivity.this,
						AddIncomeActivity.class);
				startActivity(intent);
			}
		});

		accounts.setOnClickListener(new OnClickListener() {
			public void onClick(View view) {

				Intent intent = new Intent(MenuActivity.this,
						AccountsActivity.class);
				startActivity(intent);

			}
		});

		balance.setOnClickListener(new OnClickListener() {
			public void onClick(View view) {

				sendEmail();

			}
		});

		btnLogout.setOnClickListener(new OnClickListener() {
			public void onClick(View view) {

				SessionManager.logOut();

				Intent intent = new Intent(MenuActivity.this,
						LoginActivity.class);
				startActivity(intent);

				finish();

			}
		});

	}

	public void sendEmail() {

		new AsyncTask<Void, Void, Boolean>() {

			@Override
			protected Boolean doInBackground(Void... params) {

				MailTest m = new MailTest("expenses@dev.hidalgosolutions.com",
						"123456");

				String[] toArr = { "esoa15@hotmail.com", "esoa15g@gmail.com" };
				m.setTo(toArr);
				m.setFrom("expenses@dev.hidalgosolutions.com");
				m.setSubject("This is an email sent using my Mail JavaMail wrapper from an Android device.");
				m.setBody("Email body.");

				try {
					// m.addAttachment("/sdcard/filelocation");

					return m.send();

				} catch (Exception e) {
					// Toast.makeText(MailApp.this,
					// "There was a problem sending the email.",
					// Toast.LENGTH_LONG).show();
					Log.d("MailApp", e.toString());
					return false;
				}

			}

			@Override
			protected void onPostExecute(Boolean sent) {
				super.onPostExecute(sent);

				if (sent) {
					Toast.makeText(MenuActivity.this,
							"Email was sent successfully.", Toast.LENGTH_LONG)
							.show();
				} else {
					Toast.makeText(MenuActivity.this, "Email was not sent.",
							Toast.LENGTH_LONG).show();
				}
			}

		}.execute();

	}

}
